﻿/*
 * 
 * ChartPart for SharePoint
 * ------------------------------------------
 * Copyright (c) 2008-2009, Wictor Wilén
 * http://www.codeplex.com/ChartPart/
 * http://www.wictorwilen.se/
 * ------------------------------------------
 * Licensed under the Microsoft Public License (Ms-PL) 
 * http://www.opensource.org/licenses/ms-pl.html
 * 
 */


using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace MSChartControls {
    public class MSChartControlsFeatureReceiver : SPFeatureReceiver {

        private ModificationEntry[] sections = {        
                    new ModificationEntry("appSettings", "configuration","appSettings"),
        };

        private ModificationEntry[] elements = {        
                    new ModificationEntry("add[@key='ChartImageHandler']", "configuration/appSettings","<add key='ChartImageHandler' value='storage=memory;timeout=20;'/>"),
                    new ModificationEntry("add[@path='ChartImg.axd']","configuration/system.web/httpHandlers", "<add verb='*' path='ChartImg.axd' type='System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral,PublicKeyToken=31bf3856ad364e35' validate='false'/>")

        };


        public override void FeatureActivated(SPFeatureReceiverProperties properties) {
            addOrRemove(properties, true);
        }



        
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
            addOrRemove(properties, false);
        }

        private SPWebConfigModification createModification(string name, string xPath, string value, Microsoft.SharePoint.Administration.SPWebConfigModification.SPWebConfigModificationType modType) {
            SPWebConfigModification modification = new SPWebConfigModification(name, xPath);
            modification.Owner = this.GetType().FullName;
            modification.Sequence = 0;
            modification.Type = modType;
            modification.Value = value;
            return modification;
        }

        private void addOrRemove(SPFeatureReceiverProperties properties, bool add) {
            if (properties == null)
                throw new ArgumentNullException("properties");

            SPWebApplication app = null;
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) {
                SPWeb web = properties.Feature.Parent as SPWeb;
                if (web != null) {
                    app = web.Site.WebApplication;
                }
                else {
                    app = properties.Feature.Parent as SPWebApplication;
                }
            }
            else {
                app = site.WebApplication;
            }

            if (app == null) {
                throw new Exception("Web Application is null");
            }

            if (add) {
                foreach (ModificationEntry item in sections) {
                    app.WebConfigModifications.Add(createModification(item.Name, 
                                                       item.XPath, 
                                                       item.Value, 
                                                       SPWebConfigModification.SPWebConfigModificationType.EnsureSection));
                }
                foreach (ModificationEntry item in elements) {
                    app.WebConfigModifications.Add(createModification(item.Name, 
                                                       item.XPath, 
                                                       item.Value, 
                                                       SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode));
                }
                app.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                app.Update();


            }
            else {
                Collection<SPWebConfigModification> coll = app.WebConfigModifications;
                int count = coll.Count;
                for (int c = count-1; c>=0; c--) {
                    SPWebConfigModification mod = coll[c];
                    if (mod.Owner == this.GetType().FullName) {
                        coll.Remove(mod);
                    }
                }
                if (coll.Count != count) {
                    app.Update();
                    app.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                    
                }
                app.Update();
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties) {            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {            
        }

        private struct ModificationEntry {
            public string Name;
            public string XPath;
            public string Value;
            public ModificationEntry(string Name, string XPath, string Value) {
                this.Name = Name;
                this.XPath = XPath;
                this.Value = Value;
            }
        }
    }
}
